package com.zhang.sdk.receiver;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/*****************************************************
 * 版权信息: 2019 北京振中电子有限公司版权所有
 * 创建作者: zhang.x
 * 创建日期: 2019/10/14 18:05
 * ****************************************************
 * 更改记录: 更新人:    更新时间:    更新概要:
 *
 * ****************************************************
 *  类功能说明: 蓝牙广播监听类
 * ****************************************************/
public class BluetoothStateBroadcastReceive extends BroadcastReceiver {
    private OnBluetoothStateChangedListener listener;

    public void setBluetoothStateChangedListener(OnBluetoothStateChangedListener listener) {
        this.listener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            switch (action){
                case BluetoothDevice.ACTION_ACL_CONNECTED:
                    if (listener != null) listener.onConnected(device);
                    break;
                case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                    if (listener != null) listener.onDisConnected(device);
                    break;
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    switch (blueState){
                        case BluetoothAdapter.STATE_OFF:
                            if (listener != null) listener.onStateOff();
                            break;
                        case BluetoothAdapter.STATE_ON:
                            if (listener != null) listener.onStateOn();
                            break;
                    }
                    break;
            }
        }
    }

    public static class OnBluetoothStateChangedListener {
        //设备成功连接
        public void onConnected(BluetoothDevice device){}
        //设备断开连接
        protected void onDisConnected(BluetoothDevice device){}
        //系统蓝牙开启
        protected void onStateOn(){}
        //系统蓝牙关闭
        protected void onStateOff(){}
    }
}
